﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using DataFiller.DataStorage.Extension.ExcelOperator;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace DataFiller.ExportWithNpoi
{
    public class NBook : IExcelbook
    {
        IWorkbook _book;

        #region constructors

        public NBook(string path)
        {
            _book = new XSSFWorkbook(path);
        }
        public NBook(Stream stream)
        {
            _book = new XSSFWorkbook(stream);
        }
        public NBook(XSSFWorkbook book)
        {
            _book = book;
        }

        #endregion

        #region IExcelbook

        public void Write(MemoryStream ms)
        {
            _book.Write(ms);
        }
        public IEnumerator<IExcelSheet> GetEnumerator()
        {
            foreach (ISheet sheet in _book)
            {
                yield return new NSheet(sheet);
            }
        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
        public void RemoveSheetByName(string sheetName)
        {
            int sheetIndex = _book.GetSheetIndex(sheetName);
            _book.RemoveSheetAt(sheetIndex);
        }
        public IExcelSheet CreateSheet(string sheetName)
        {
            return new NSheet(_book.CreateSheet(sheetName));
        }

        public IExcelSheet GetSheet(string sheetName)
        {
            return new NSheet(_book.GetSheet(sheetName));
        }
        #endregion
    }
}
